#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QDir>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget),snifferT(nullptr)
    , tableHeaders({"Time","Source","Destination","Protocol","Length","Info","Data"})
{
    ui->setupUi(this);
    //设置ip地址的输入框
    ui->lineEdit->setInputMask(QString("000.000.000.000"));
    ui->lineEdit_2->setInputMask(QString("000.000.000.000"));

    //设置表格
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setHorizontalHeaderLabels(tableHeaders);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setColumnHidden(6,true);

    //不同系统分辨率下可能需要调整
    this->resize(QSize(1000,600));
}

Widget::~Widget()
{
    delete ui;
    if(snifferT->isRunning()){
        snifferT->quit();
        snifferT=nullptr;
    }
}


void Widget::on_pushButton_clicked()
{
    QStringList ads = sniffer.getAdapters();
    ui->comboBox->clear();
    ui->comboBox->addItems(ads);
}


void Widget::on_pushButton_2_clicked()
{
    int i = ui->comboBox->currentIndex();
    if(i<0)
        return;
    snifferT = QThread::create(&Sniffer::beginSniff,sniffer.alldevs,i);
    sniffer.doing = true;
    snifferT->start();
    if(snifferT->isRunning()){
        qDebug()<<"new Thread created to sniff";
        ui->pushButton_2->setDisabled(true);
    }else {
        qDebug()<<"Fail to create new Thread";
    }
}


void Widget::on_pushButton_3_clicked()
{
    //snifferT=nullptr;
    if(snifferT!=nullptr){
        sniffer.doing = false;
        QTest::qSleep(500);
        if(snifferT->isFinished()){
            qDebug()<<"Thread finished";
            ui->pushButton_2->setDisabled(false);
        }else {
            qDebug()<<"Fail to finish Thread";
        }
    }
    QDir dir(".");
    QStringList dumps=dir.entryList(Sniffer::fileSuffix,QDir::NoFilter,QDir::Time);
    //qDebug()<<"dump files:" <<dumps;
    ui->comboBox_2->clear();
    ui->comboBox_2->addItems(dumps);
}


void Widget::on_pushButton_4_clicked()
{
    qDebug()<<ui->comboBox_3->currentText();
    qDebug()<<ui->lineEdit->text()<<":"<<ui->spinBox->value()<<" --> "<<
              ui->lineEdit_2->text()<<":"<<ui->spinBox_2->value();
    int nOldRowCount = ui->tableWidget->rowCount();
    for(int i=nOldRowCount-1; i>=0; --i){
        ui->tableWidget->removeRow(i);
    }
    QStringList filters;
    filters.append(ui->comboBox_3->currentText());
    filters.append(ui->lineEdit->text());
    filters.append(QString::number(ui->spinBox->value()));
    filters.append(ui->lineEdit_2->text());
    filters.append(QString::number(ui->spinBox_2->value()));
    sniffer.readDump(ui->comboBox_2->currentText(),ui->tableWidget,filters);
    ui->tableWidget->resizeColumnToContents(5);
}


void Widget::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
    if(current!=nullptr){
        QString data = ui->tableWidget->item(current->row(),6)->text();
        ui->textBrowser->setText(data);
    }
}

